package jmu.cze.service.impl;

import jmu.cze.mapper.AirTicketMapper;
import jmu.cze.mapper.OrderMapper;
import jmu.cze.mapper.PassengerMapper;
import jmu.cze.mapper.TicketTypeMapper;
import jmu.cze.pojo.AirTicket;
import jmu.cze.pojo.Order;
import jmu.cze.pojo.Passenger;
import jmu.cze.pojo.TicketType;
import jmu.cze.service.BookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BookServiceImpl implements BookService {
    private PassengerMapper passengerMapper;
    private OrderMapper orderMapper;
    private AirTicketMapper airTicketMapper;
    private TicketTypeMapper ticketTypeMapper;

    @Resource(name = "passengerMapper")
    public void setPassengerMapper(PassengerMapper passengerMapper) {
        this.passengerMapper = passengerMapper;
    }

    @Resource(name = "orderMapper")
    public void setOrderMapper(OrderMapper orderMapper) {
        this.orderMapper = orderMapper;
    }

    @Resource(name = "airTicketMapper")
    public void setAirTicketMapper(AirTicketMapper airTicketMapper) {
        this.airTicketMapper = airTicketMapper;
    }

    @Resource(name = "ticketTypeMapper")
    public void setTicketTypeMapper(TicketTypeMapper ticketTypeMapper) {
        this.ticketTypeMapper = ticketTypeMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // TODO:事务管理
    public boolean book(Order order, Integer ticketTypeID, List<Passenger> passengers) {
        boolean flag;
        AirTicket airTicket;
        TicketType ticketType = ticketTypeMapper.selectByID(ticketTypeID);

        flag = orderMapper.insert(order) == 1; // useGeneratedKeys="true" 插入成功后将设置orderID=主键id
        for (Passenger passenger : passengers) {
            if (passengerMapper.selectByID(passenger.getPassengerID()) == null) {
                flag = passengerMapper.insert(passenger) == 1 && flag;
            } else {
                flag = passengerMapper.update(passenger) == 1 && flag;
            }
            airTicket = new AirTicket();
            airTicket.setPassengerID(passenger.getPassengerID());
            airTicket.setOrderID(order.getOrderID());
            airTicket.setTicketTypeID(ticketTypeID);
            airTicket.setConstructionCosts(50.0);
            airTicket.setInsuranceCosts(0.0);
            airTicket.setTotalCosts(ticketType.getBasicPrice() + airTicket.getConstructionCosts() + airTicket.getInsuranceCosts());
            airTicket.setLuggageCount(0);
            airTicket.setMealType("普通餐食");
            airTicket.setBookTime(new Date());
            System.out.println(airTicket);
            flag = airTicketMapper.insert(airTicket) == 1 && flag;
            order.setOrderCosts(order.getOrderCosts() + airTicket.getTotalCosts());
        }
        orderMapper.update(order);
        return flag;
    }

    @Override
    public List<AirTicket> selectAirTicketsByOrderID(Integer orderID) {
        return airTicketMapper.selectAirTicketsByOrderID(orderID);
    }

    @Override
    public Order selectOrderByID(Integer orderID) {
        return orderMapper.selectByID(orderID);
    }

    @Override
    public boolean updateOrder(Order order) {
        return orderMapper.update(order)==1;
    }
}
